package org.snp

import au.com.bytecode.opencsv.bean._
import java.io.FileReader
import java.io.File

//read in an activate CSV file to a List of addresses
class VoterCSVReader(voterFile: File) {
  var voters: List[Voter] = Nil
  readVoters

  private def readVoters() {
    val strat = new ColumnPositionMappingStrategy()
    strat.setType(classOf[Voter].asInstanceOf[Class[Nothing]]) //don't ask me, setType is parameterised, why does it insist on Nothing then?
    //TODO make this less brittle - should be possiblefor activate to add columns without affecting this slurp
    val columns = Array[String]("id", "registerNum", "salutation", "firstName", "middleInitial", "lastName", "gender",
      "homePhone", "postal", "snpCategory", "aline1", "aline2", "village", "district", "city", "postcode", "leafletCode") // the fields to bind do in your JavaBean
    strat.setColumnMapping(columns)

    val csv = new CsvToBean()
    val list = csv.parse(strat, new FileReader(voterFile))
    voters = List.fromArray(list.toArray(new Array[Voter](0))).tail //usual "make a scala list" nastyness
  }
}
